package com.itheima.reggie.common;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.itheima.reggie.entity.Pay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class AliPayServer {
    @Value("${alipay.appId}")
    private String appId;
    @Value("${alipay.privateKey}")
    private String privateKey;
    @Value("${alipay.appPublicKey}")
    private String appPublicKey;
    @Value("${alipay.alipayPublicKey}")
    private String alipayPublicKey;

    //支付方法
    //returnUrl为支付后跳转
    //pay封装支付订单信息
    public String pay(String returnUrl, String notifyUrl, Pay pay){
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                appId,
                privateKey,
                "json",
                "utf-8",
                appPublicKey,
                "RSA2");
        //创建API对应的request
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\""+pay.getOrderId()+"\"," +
                " \"total_amount\":\""+pay.getPrice()+"\"," +
                " \"subject\":\""+pay.getSubject()+"\"," +
                " \"product_code\":\""+pay.getProductId()+"\"" +
                " }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }
    public String pay(String returnUrl, Pay pay){
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                appId,
                privateKey,
                "json",
                "utf-8",
                appPublicKey,
                "RSA2");
        //创建API对应的request
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl("");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\""+pay.getOrderId()+"\"," +
                " \"total_amount\":\""+pay.getPrice()+"\"," +
                " \"subject\":\""+pay.getSubject()+"\"," +
                " \"product_code\":\""+pay.getProductId()+"\"" +
                " }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }

    //查询支付结果方法
    public boolean notifyPay(Map<String,String> map) throws AlipayApiException {

        boolean signVerified = AlipaySignature.rsaCheckV1(map,
                alipayPublicKey,
                "utf-8",
                "RSA2");//调用SDK验证签名
       return signVerified;
    }
}
